<?
// Create 26/5/2011
// suongmumc@gmail.com


defined( '_VALID_NVB' ) or die( 'Direct Access to this location is not allowed.' );
function write_link(){
	global $DB,$CONFIG;
	
	if($CONFIG['dir_path']=='')
		$rootpath='/';
	else
		$rootpath=$CONFIG['dir_path'];
	$File = "../".".htaccess"; 
	$Handle = fopen($File, 'w');
	$Data = "
	# Power By Nguyen Binh
	# suongmumc@gmail.com
	
	RewriteEngine on 
	RewriteBase ".$rootpath."
	RewriteCond %{REQUEST_FILENAME} !-f 
	RewriteCond %{REQUEST_FILENAME} !-d  
	
	RewriteRule ^style/(.*) css/$1
	RewriteRule ^java/(.*) js/$1
	RewriteRule ^img/(.*) images/$1
	RewriteRule ^home/$ index.php?page=home 
	RewriteRule ^trang-chu.html$ index.php?page=home 
	RewriteRule ^lien-he.html$ index.php?page=contact&title=lien-he 
	
	
	"; 
	
	$arrtable=array(
		'cat' => 'id_cat',
		'catpd' => 'id_catpd',
		'catpt' => 'id_catpt',
		'catif' => 'id_catif'
	);
	
	foreach ($arrtable as $table => $id_cat){
		$sql="SELECT * FROM $table ORDER BY name DESC";
		$db=$DB->query($sql);
		switch($table){
			case 'cat':
				$mod='news';
				break;
			case 'catpd':
				$mod='products';
				break;
			case 'catpt':
				$mod='gallery';
				break;
			case 'catif':
				$mod='info';
				break;
				
		}
$Data.='# ----------------- '.$mod.'
		';
		while($rs=mysql_fetch_array($db)){
			$DB->query('UPDATE '.$table.' SET url="'.category_link_name($table,$id_cat,$rs[$id_cat]).'/'.'" WHERE '.$id_cat.'='.$rs[$id_cat]);	
			$Data.='RewriteRule ^'.category_link_name($table,$id_cat,$rs[$id_cat]).'/$ index.php?page='.$mod.'&idc='.$rs[$id_cat].'&title='.clean_url($rs['name']).'
			';
			$Data.='RewriteRule ^'.category_link_name($table,$id_cat,$rs[$id_cat]).'/([0-9]*).htm$ index.php?page='.$mod.'&idc='.$rs[$id_cat].'&title='.clean_url($rs['name']).'&p=$1			
			';
			$Data.='RewriteRule ^'.category_link_name($table,$id_cat,$rs[$id_cat]).'/(.*)_([0-9]*).html$ index.php?page='.$mod.'&idc='.$rs[$id_cat].'&title=$1&id=$2
			';
			
			;
		}
		
	}
	$ok=fwrite($Handle, $Data); 
	fclose($Handle); 	
}

function category_link_name($table,$id_cat,$idc){
	global $DB;
	$idc=intval($idc);
	$sql="SELECT * FROM $table WHERE $id_cat=$idc";
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$str=clean_url($rs['name']);
		$sql1="SELECT * FROM $table WHERE $id_cat=$rs[parentid]";
		$db1=$DB->query($sql1);
		$i=0;
		while($rs1=mysql_fetch_array($db1))	{
			$str=clean_url($rs1['name'])."/".$str;
			$i++;
		}
		if($i==0) $str='home/'.$str;
	}
	return $str;
}


?>